﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WLG.Model.Interface;
using WLG.Model.NotifyContext;
using MSNPSharp.MSNWS.MSNABSharingService;
using WLG.Model.Permissions;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using WLG.NotifyLiveDataProvider.PersonalFeed;
using System.Net;
using System.Xml.Serialization;

namespace WLG.NotifyLiveDataProvider.SocialFeed
{
    public class LiveSocialDataProvider : INotifyDataProvider
    {
        public List<Notification> GetNotifications(Identity ident)
        {
            Uri FeedUrl = NotificationAdapter.GetNotificationsFeedUrl(ident);
            return GetNotifications(FeedUrl);
        }

        private List<Notification> GetNotifications(Uri feedUrl)
        {
            List<Notification> results = new List<Notification>();

            LiveServicesResponseTypes.feed NotifyFeedXML = ScrapeFeed(feedUrl);

            foreach (LiveServicesResponseTypes.feedEntry r in NotifyFeedXML.entry)
            {
                Notification n = new Notification();
                results.Add(n);
                n.ID = r.id;
                n.Title = r.title;
                n.Updated = r.updated;
                n.LiveID = r.liveid;

                LiveServicesResponseTypes.feedEntrySummary sr = r.summary;
                n.Summary = new NotifySummary();
                n.Summary.SummaryText = sr.Value;
                n.Summary.Type = sr.type;


                LiveServicesResponseTypes.feedEntryContent cr = r.content;

                n.Content = new NotifyContent();
                n.Content.Corpus = cr.Value;
                n.Content.Type = cr.type;


                LiveServicesResponseTypes.feedEntryLink lr = r.link;

                n.Link = new NotifyLink();
                n.Link.Href = lr.href;
            }

            return results;
        }

        private static LiveServicesResponseTypes.feed ScrapeFeed(Uri Url)
        {
            LiveServicesResponseTypes.feed result = new LiveServicesResponseTypes.feed();
            HttpWebRequest WebRequestObject = null;
            WebResponse Response = null;
            Stream WebStream = null;
            try
            {

                WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(Url);

                WebRequestObject.UserAgent = ".NET Framework/3.5";
                WebRequestObject.Referer = "http://www.deepelement.com/blog";


                Response = WebRequestObject.GetResponse();


                StreamReader loResponseStream = new
                      StreamReader(Response.GetResponseStream());
                string content = loResponseStream.ReadToEnd().Replace("live:id", "liveid");
                StringReader sr = new StringReader(content);
                XmlSerializer xser = new XmlSerializer(typeof(LiveServicesResponseTypes.feed));
                result = (LiveServicesResponseTypes.feed)xser.Deserialize(sr);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (Response != null)
                    Response.Close();
            }
            return result;
        }
    }
}
